import request from "../request";
import type { Response, Omit } from "../types";
import type { RegisterInfo, RegisterUserInfo } from "./type";

const register: Response<
  RegisterInfo & RegisterUserInfo,
  Record<string, never>
> = params => {
  return request.post("/user/register", params);
};

const login: Response<
  Omit<RegisterInfo, "confirmPassword">,
  Record<string, never>
> = params => {
  return request.post("/user/login", params);
};

const getUserInfo: Response<
  { [key: string]: string },
  Record<string, never>
> = params => {
  return request.get("/user/userinfo", {
    params: params
  });
};

const UpdataUserInfo: Response<
  {
    userid: number;
    avatar: string;
    username: string;
    sex: number;
    email: string;
    userSignature?: string;
    verifyCode?: string;
  },
  Record<string, never>
> = params => {
  return request.post("/user/updatauser", params);
};

// 添加更新用户签名的接口
const updateUserSignature: Response<
  {
    userId: number;
    userSignature: string;
  },
  Record<string, never>
> = params => {
  return request.post("/user/updatesignature", params);
};

// 发送邮箱验证码
const sendEmailVerifyCode = (email: string, type: "register" | "update" = "register") => {
  return request.post("/user/sendEmailCode", { email, type });
};

// 验证邮箱验证码
const checkEmailVerifyCode = (email: string, verifyCode: string, type: "register" | "update" = "register") => {
  return request.post("/user/checkEmailCode", { email, verifyCode, type });
};

// 重置密码
const resetPassword = (email: string, newPassword: string) => {
  return request.post("/user/resetPassword", { email, newPassword });
};

// 检查邮箱是否已被使用
const checkEmailExists = (email: string, currentUserId?: number) => {
  return request.post("/user/checkEmailExists", { email, currentUserId });
};

export { 
  register, 
  login, 
  getUserInfo, 
  UpdataUserInfo, 
  updateUserSignature, 
  sendEmailVerifyCode, 
  checkEmailVerifyCode,
  resetPassword,
  checkEmailExists
};
